<?php
//竞争对手
class CompetitorController extends Controller
{

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_competitor where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select c.* from crm_competitor c where c.deleted = 0  limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $competitor_id = $_REQUEST['competitor_id'];

        $competitor = null;
        if($competitor_id==0){
            //新增
        }else{
            //编辑
            $competitor = $this->connection->createCommand("select c.* from crm_competitor c where
 c.competitor_id = :competitor_id")->bindParam(':competitor_id',$competitor_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['competitor'=>$competitor]);
    }

    public function actionSavedata(){

        $competitor_id = $_REQUEST['competitor_id'];
        $competitor_name = $_REQUEST['competitor_name'];
        $advantage = $_REQUEST['advantage'];
        $power = $_REQUEST['power'];

        if($competitor_id!=null){
            //编辑
            $this->connection->createCommand("update crm_competitor set competitor_name = :competitor_name,advantage = :advantage
,power = :power where competitor_id = :competitor_id")
                ->bindParam(':competitor_id',$competitor_id,PDO::PARAM_INT)
                ->bindParam(':competitor_name',$competitor_name,PDO::PARAM_STR)
                ->bindParam(':advantage',$advantage,PDO::PARAM_STR)
                ->bindParam(':power',$power,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_competitor (competitor_name,advantage,power) values (:competitor_name,:advantage,:power) ")
                ->bindParam(':competitor_name',$competitor_name,PDO::PARAM_STR)
                ->bindParam(':advantage',$advantage,PDO::PARAM_INT)
                ->bindParam(':power',$power,PDO::PARAM_STR)
                ->execute();
        }
    }


    public function actionRemove(){
        $competitor_id = $_REQUEST['competitor_id'];
        $this->connection->createCommand("update crm_competitor set deleted = 1 
where competitor_id = :competitor_id")
            ->bindParam(':competitor_id',$competitor_id,PDO::PARAM_INT)
            ->execute();
    }

}